package com.acme;

import com.acme.bankapp.domain.bank.AbstractAccount;
import com.acme.bankapp.domain.bank.Account;
import com.acme.bankapp.domain.bank.Bank;
import com.acme.bankapp.domain.bank.CheckingAccount;
import com.acme.bankapp.domain.bank.Client;
import com.acme.bankapp.domain.bank.Gender;
import com.acme.bankapp.domain.bank.SavingsAccount;
import com.acme.bankapp.service.bank.BankService;

public class BankApplication {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Bank bank = new Bank();
		
		fillClients(bank);
		
		printBalance(bank);
		modifyBank(bank);
		printBalance(bank);
		
		BankService.printMaximumAmountToWithdraw(bank);
	}
	
	static void fillClients(Bank bank) {
		try {
			BankService.addClient(bank, new Client("Pete", Gender.MALE, new SavingsAccount(50)));
			BankService.addClient(bank, new Client("Mary", Gender.FEMALE, new CheckingAccount(40, 10)));
			BankService.addClient(bank, new Client("John", Gender.MALE, new SavingsAccount(60)));
			BankService.addClient(bank, new Client("Katy", Gender.FEMALE, new CheckingAccount(70, 30)));
			BankService.addClient(bank, new Client("Sean", Gender.MALE, new SavingsAccount(30)));
			BankService.addClient(bank, new Client("Odry", Gender.FEMALE, new CheckingAccount(80, 15)));
			BankService.addClient(bank, new Client("Steven", Gender.MALE, new SavingsAccount(20)));
			BankService.addClient(bank, new Client("Taylor", Gender.FEMALE, new CheckingAccount(90, 50)));
			BankService.addClient(bank, new Client("Mike", Gender.MALE, new SavingsAccount(10)));
			BankService.addClient(bank, new Client("Valery", Gender.FEMALE, new CheckingAccount(0, 20)));
			BankService.addClient(bank, new Client("Jessica", Gender.FEMALE, new SavingsAccount(120)));
		}
		catch (Exception ex) {
			System.out.println("Error while creating array of clients: " + ex.getMessage());
		}
	}
	
	static void modifyBank(Bank bank) {
		for (Client client : bank.getClients()) {
			for (Account account : client.getAccounts()) {
				try {
					account.withDraw(15);
					account.deposit(10);
				}
				catch (Exception ex) {
					System.out.println("Error while changing balance of client <" + client.getClientSalutation() + ">: " + ex.getMessage());
				}
			}
		}
	}
	
	static void printBalance(Bank bank) {
		System.out.println("Bank clients info:");
		for (Client client : bank.getClients()) {
			System.out.print(String.format("Client <%s>: ", client.getClientSalutation()));
			for (Account account : client.getAccounts()) {
				System.out.println(account);
			}
		}
	}
}
